package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	utils "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/bid"
	reportService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type BidController struct {
	controller.BaseController
	bidService    bid.BidServiceIface
	reportService reportService.ReportServiceIface
}

// NewBidController
// 招中标
func NewBidController(
	bidService bid.BidServiceIface,
	reportService reportService.ReportServiceIface,
) *BidController {
	return &BidController{
		bidService:    bidService,
		reportService: reportService,
	}
}

// RegisterRouter
// 注册路由
func (c *BidController) RegisterRouter(g *gin.RouterGroup) {
	//找招中标
	g.POST("/bid/search-bid", c.searchBid)
	//找招中标 - 联想
	g.GET("/bid/fuzzy-search-bid", c.fuzzySearchBid)
	//招标详情
	g.POST("/bid/bid-detail", c.BidDetail)
	//招标详情-手机号明文
	g.POST("/bid/bid-detail-mobile", c.BidDetailMobile)
	//中标详情
	g.POST("/bid/winbid-detail", c.WinBidDetail)
}

func (c *BidController) SetAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *BidController) searchBid(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	req := &dto.SearchBidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.bidService.SearchBid(ctx, req, 1)
	if err != nil {
		stark.Logger.Errorf(ctx, "bid/search-bid err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *BidController) fuzzySearchBid(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	req := &dto.FuzzySearchBidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.bidService.FuzzySearchBid(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "bid/fuzzy-search-bid err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *BidController) BidDetail(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	req := &dto.BidDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.bidService.BidDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "bid/bid-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *BidController) BidDetailMobile(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	req := &dto.BidDetailMobileRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.bidService.BidDetailMobile(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "bid/bid-detail-mobile err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *BidController) WinBidDetail(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	req := &dto.WinBidDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.bidService.WinBidDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "bid/winbid-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}
